<template>
  <div class="h-full w-full flex flex-col justify-between">
    <div class="form px-[15px] pt-[15px] border-bottom">
      <el-form>
        <el-row :gutter="12">
          <el-col :span="4">
            <el-form-item>
              <el-input v-model="form.name" placeholder="用户名/昵称/备注" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <div class="w-full flex justify-between">
                <el-date-picker
                  v-model="form.date"
                  type="daterange"
                  unlink-panels
                  range-separator="－"
                  start-placeholder="选择开始时间"
                  end-placeholder="选择结束时间"
                  :disabled-date="disableFutureDates"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item>
              <el-button type="primary" @click.stop="search">搜索</el-button>
              <el-button type="danger" @click.stop="handleRefresh">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="px-[15px] pt-[10px]">
      <el-button type="success" @click="handleBtnDate('today')">今天</el-button>
      <el-button type="success" @click="handleBtnDate('yesterday')">昨天</el-button>
      <el-button type="success" @click="handleBtnDate('thisWeek')">本周</el-button>
      <el-button type="success" @click="handleBtnDate('lastWeek')">上周</el-button>
      <el-button type="success" @click="handleBtnDate('thisMonth')">本月</el-button>
      <el-button type="success" @click="handleBtnDate('lastMonth')">上月</el-button>
    </div>
    <div class="grow-[1] h-0 mx-[15px] mt-[10px]">
      <el-table
        scrollbar-always-on
        ref="elTableRef"
        v-loading="state.loading"
        :data="list"
        style="width: 100%"
        height="100%"
        border
        stripe
        fit
      >
        <template #empty>
          <ui-empty />
        </template>
        <el-table-column align="center" prop="Username" label="用户名">
          <template #default="{ row }: TableItem">
            {{ row.Username || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="CreatedAt" label="投注时间" width="170">
          <template #default="{ row }: TableItem">
            {{ formatDate(row.CreatedAt) || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="LotteryName" label="投注彩种">
          <template #default="{ row }: TableItem">
            {{ row.LotteryName || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Cat2" label="投注玩法" show-overflow-tooltip>
          <template #default="{ row }: TableItem"> {{ row.Cat2 }}-{{ row.Cat3 }} </template>
        </el-table-column>
        <el-table-column align="center" prop="BetAmount" label="投注期号">
          <template #default="{ row }: TableItem">
            {{ row.BitchNo || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="BetAmount" label="投注金额">
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.BetAmount"></ui-amount>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="WinAmount" label="中奖金额">
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.WinAmount"></ui-amount>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex justify-between my-[10px] mx-[15px]">
      <div class="flex-1 w-0 mr-[20px] flex justify-start">
        <div>
          <el-alert
            title="注意：报表最多保留两月数据。"
            type="warning"
            show-icon
            :closable="false"
          />
        </div>
      </div>
      <ui-pagination
        :total="state.total"
        v-model:pageSize="state.pageSize"
        v-model:currentPage="state.page"
        @current-change="getData"
      ></ui-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import moment from "moment";
import { useList } from "@/hooks/useList";
import { reqReportNotice } from "@/api/report";
import { disableFutureDates, formatDate, getDateArray } from "@/utils/common";
import $bus from "@/utils/bus";

interface Params {
  StartDate: string;
  EndDate: string;
  Name?: string;
}
interface TableItem {
  row: ReportNoticeMo;
}

const form = reactive({
  name: "",
  date: getDateArray()["today"]
});
const selectDate = ref<string>("today");
const getDate = () => {
  const date: Record<string, string[]> = getDateArray();
  return date;
};
const getParams = () => {
  const params: Params = {
    StartDate: moment(form.date[0]).format("YYYY-MM-DD 00:00:00"),
    EndDate: moment(form.date[1]).format("YYYY-MM-DD 23:59:59")
  };
  if (form.name) {
    params.Name = form.name;
  }
  return params;
};
const { elTableRef, state, list, getData, search } = useList<ReportNoticeMo, Params>(
  reqReportNotice,
  getParams
);

// 重置
const handleRefresh = () => {
  form.name = "";
  form.date = getDateArray()["today"];
  search();
};

const handleBtnDate = (str: string) => {
  const date = getDate();
  selectDate.value = str;
  form.date = date[str];
  search();
};

onMounted(() => {
  getData();
  $bus.on("update-report-notice-msg", () => {
    getData();
  });
});
</script>
